package cn.itcast.service.impl;

import cn.itcast.dao.cargo.ContractDao;
import cn.itcast.dao.cargo.ExtCproductDao;
import cn.itcast.domain.cargo.Contract;
import cn.itcast.domain.cargo.ExtCproduct;
import cn.itcast.domain.cargo.ExtCproductExample;
import cn.itcast.service.ExtCproductService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.UUID;

@Service
public class ExtCproductServiceImpl implements ExtCproductService {

    @Autowired
    private ExtCproductDao extCproductDao;
    @Autowired
    private ContractDao contractDao;

    @Override
    public PageInfo findAll(ExtCproductExample example, int page, int size) {

        PageHelper.startPage(page,size);
        List<ExtCproduct> list = extCproductDao.selectByExample(example);
        PageInfo<ExtCproduct> pb = new PageInfo<>(list);
        return pb;
    }

    @Override
    public void save(ExtCproduct ExtCproduct) {
        // 1 设置一些附件的额外属性（uuid 总金额...）
        ExtCproduct.setId(UUID.randomUUID().toString());
        //附件的总金额==附件的单价*附件的数量
        Double total=ExtCproduct.getPrice()*ExtCproduct.getCnumber();
        ExtCproduct.setAmount(total);
        //2 保存附件的数据到数据库
        extCproductDao.insertSelective(ExtCproduct);
        //3 根据附件的所属合同id查询合同
        Contract contract = contractDao.selectByPrimaryKey(ExtCproduct.getContractId());
        //4 修改合同的总金额
        contract.setTotalAmount(contract.getTotalAmount()+total);
        //5 修改合同的附件数
        contract.setExtNum(contract.getExtNum()+1);
        //6 修改合同数据到数据库
        contractDao.updateByPrimaryKeySelective(contract);
    }

    @Override
    public void update(ExtCproduct extCproduct) {
        //1 根据附件的id获取到以前的附件对象
        ExtCproduct old_extCproduct = extCproductDao.selectByPrimaryKey(extCproduct.getId());
        //2 获取附件以前的总金额
        Double old_total=old_extCproduct.getAmount();
        //3 计算附件修改之后的总金额
        Double new_total=extCproduct.getPrice()*extCproduct.getCnumber();
        extCproduct.setAmount(new_total);
        //4 修改附件数据到数据库
        extCproductDao.updateByPrimaryKeySelective(extCproduct);

        //5 根据附件的合同id获取到合同
        Contract contract = contractDao.selectByPrimaryKey(extCproduct.getContractId());
        //6 修改合同的总金额
        contract.setTotalAmount(contract.getTotalAmount()-old_total+new_total);
        //7 修改合同最新数据到数据库
        contractDao.updateByPrimaryKeySelective(contract);
    }

    @Override
    public void delete(String id) {

        //附件变
            //获取到附件对象
        ExtCproduct extCproduct = extCproductDao.selectByPrimaryKey(id);
            // 获取附件以前的金额
        Double total=extCproduct.getAmount();
          // 删除附件
        extCproductDao.deleteByPrimaryKey(id);
        //合同变
            // 根据附件的合同id获取到合同
        Contract contract = contractDao.selectByPrimaryKey(extCproduct.getContractId());
        // 合同设置金额
        contract.setTotalAmount(contract.getTotalAmount()-total);
            // 合同设置附件数
        contract.setExtNum(contract.getExtNum()-1);
            // 修改合同数据到数据库
        contractDao.updateByPrimaryKeySelective(contract);

    }

    @Override
    public ExtCproduct findById(String id) {

        return extCproductDao.selectByPrimaryKey(id);
    }


}
